/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'upload', 'laydate', 'jquery'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let upload = layui.upload;
    let index;
    let timestamp = new Date().getTime();
    if (json.productGuarantee.productGuaranteeId > 0) {
        reLoadImage("smallPictureOne", json.productGuarantee.smallPictureOne, "smallPictureOneValue");
        reLoadImage("smallPictureTwo", json.productGuarantee.smallPictureTwo, "smallPictureTwoValue");
        reLoadImage("smallPictureThree", json.productGuarantee.smallPictureThree, "smallPictureThreeValue");
        reLoadImage("smallPictureFour", json.productGuarantee.smallPictureFour, "smallPictureFourValue");
        reLoadImage("smallPictureFive", json.productGuarantee.smallPictureFive, "smallPictureFiveValue");
        reLoadImage("smallPictureSix", json.productGuarantee.smallPictureSix, "smallPictureSixValue");
    }

    //初始化文件上传插件
    uploaderImage("smallPictureOne", "smallPictureOneValue");
    uploaderImage("smallPictureTwo", "smallPictureTwoValue");
    uploaderImage("smallPictureThree", "smallPictureThreeValue");
    uploaderImage("smallPictureFour", "smallPictureFourValue");
    uploaderImage("smallPictureFive", "smallPictureFiveValue");
    uploaderImage("smallPictureSix", "smallPictureSixValue");

    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;
        for (let k in data) {
            if (data[k] == '') {
                data[k] = null;
            }
        }
        let url = "/showProduct/guarantee/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
                });
            },
            success: function (json) {
                layer.close(index);
                layer.msg(json.message);
                if (json.status != 200) {
                    zmnButtonUtil.enableButton("submit", $);
                    /*       setTimeout(function () {
                               window.location.href="/showProduct/list.action";
                           },1000);*/
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete: function () {
                layer.close(index);
            }
        });
        return false;
    });

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId) {
        upload.render({
            elem: '#' + elemId
            , exts: 'jpg|png|jpeg|gif'
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/showProduct/picture/uploaderImage.action?timestamp=' + timestamp + '&elemId=' + elemId
            , size: 3072
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    let next2 = $(next1).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                    $(next2).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }


    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //对外暴露的接口
    exports('product_guarantee_edit_js', {});
});

function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = document.getElementById(element).value;
    // img.src = obj.src;
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px', 300 + 'px'],
        shadeClose: true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}

function deleteImg(element) {
    var elementValue = element + 'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function (item) {
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}

